//
//  Shijian_add_vc.m
//  ainkjj
//
//  Created by MAX on 2020/10/30.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Shijian_add_vc.h"
#import "WSDatePickerView.h"
#import "EventCalendar.h"

@interface Shijian_add_vc ()<UITextFieldDelegate>
@property (nonatomic, strong)UITextField *tf;
@property (nonatomic, strong)UIButton *btn;
@property (nonatomic, strong)NSDate *startDate;

@end

@implementation Shijian_add_vc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加事件";
    
    UILabel *tip1 = UILabel.new;
    [self.view addSubview:tip1];
    tip1.text = @"事件名称";
    
    tip1.frame = CGRectMake(10, 20 + Lzy_StatusBarAndNavigationBarHeight, 100, 20);
    
    UITextField *tf = UITextField.new;
    self.tf = tf;
    [self.view addSubview:tf];
    tf.placeholder = @"请输入事件名称";
    tf.frame = CGRectMake(CGRectGetMaxX(tip1.frame) + 4, tip1.mj_y-4, ScreenWidth - (CGRectGetMaxX(tip1.frame) + 4), 30);
    tf.delegate = self;
    
    {
        UILabel *tip1 = UILabel.new;
        [self.view addSubview:tip1];
        tip1.text = @"提醒时间";
        
        tip1.frame = CGRectMake(10, CGRectGetMaxY(tf.frame) + 30, 100, 20);
        
        UIButton *btn = UIButton.new;
        [self.view addSubview:btn];
        self.btn = btn;
        [btn setTitle:@"请选择日期" forState:UIControlStateNormal];
        btn.frame = CGRectMake(CGRectGetMaxX(tip1.frame), tip1.mj_y - 10, tf.mj_w, 44);
        [btn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(riqi_action:) forControlEvents:UIControlEventTouchUpInside];
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        
        UIButton *btn2 = [[UIButton alloc] init];
        [self.view addSubview:btn2];
        
        [btn2 setTitle:@"添加" forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(fabu_action:) forControlEvents:UIControlEventTouchUpInside];
        [btn2 setTitleColor:appMainColor forState:UIControlStateNormal];
        btn2.frame = CGRectMake(20, CGRectGetMaxY(btn.frame) + 30, ScreenWidth - 40, 44);
        
        btn2.layer.masksToBounds = YES;
        btn2.layer.borderColor = appMainColor.CGColor;
        btn2.layer.borderWidth = 1;
        btn2.layer.cornerRadius = 22;
    }
   
    
}
- (void)fabu_action:(UIButton *)sender{
    if ([Anktool strIsBlank:self.tf.text]) {
        [QMUITips showInfo:@"请输入事件名称"];
        return;
    }
    
    if ([self.btn.currentTitle isEqualToString:@"请选择日期"]) {
        [QMUITips showInfo:@"请选择日期"];
        return;
    }
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:self.tf.text forKey:@"content"];
    [dict setObject:self.btn.currentTitle forKey:@"date_time"];
    [dict setObject:[Anktool strBlankHolder:[self.btn.currentTitle componentsSeparatedByString:@"-"][1]] forKey:@"month"];
    //sender.userInteractionEnabled = NO;
    
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];

    [PPHTTPRequest req:@"api/pets/App/setEvent" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            [QMUITips showSucceed:@"添加成功"];

            
            /* TODO: #待完成# */;
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            [QMUITips showError:response[@"error_msg"]];
        }
        
    } failure:^(NSError *error) {
        [QMUITips showError:[error localizedDescription]];
        
    }];
    [EventCalendar.sharedEventCalendar createEKEventTitle:self.tf.text startData:self.startDate endDate:self.startDate];

    
}
- (void)riqi_action:(UIButton *)btn{
    [self.view endEditing:YES];
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDayHourMinute CompleteBlock:^(NSDate *startDate) {
        self.startDate = startDate;
        NSString *date = [startDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
        DLog(@"时间： %@",date);
        [btn setTitle:date forState:UIControlStateNormal];
        
    }];
    datepicker.minLimitDate = [NSDate.date dateByAddingHours:1];
    datepicker.doneButtonColor = appMainColor;//确定按钮的颜色
    [datepicker show];
}
#pragma mark - ================== tf delegate ==================
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSMutableString *newtxt = [NSMutableString stringWithString:textField.text];
    
    
    [newtxt replaceCharactersInRange:range withString:string];
    
    int MAX_CHARS = 36;
    
    return ([newtxt length] <= MAX_CHARS);
    
    return YES;
    
}

@end
